<template>
    <div>
        <el-dialog title="添加新器材"
                   :visible.sync="saveNewEquipShow"
                   :show-close="false"
                   :before-close="handleClose">
            <el-form label-position="right" :rules="rules" :model="EquipForm" ref="ruleForm">
                <el-form-item label="图片上传" prop="file">
                    <el-upload
                            :multiple="false"
                            ref="upload"
                            class="upload-demo"
                            action="#"
                            :auto-upload="false"
                            :file-list="HFfileList"
                            :before-upload="beforeUpload"
                            list-type="picture-card"
                            :on-preview="handlePictureCardPreview"
                            :on-remove="HFhandleRemove"
                            :on-change="HFhandleChangeImg">
                        <i class="el-icon-plus"></i>
                        <div slot="tip" class="el-upload__tip">
                            只能上传jpg/png文件，且不超过5Mb
                        </div>
                    </el-upload>
                </el-form-item>

                <el-form-item label="器材名称" prop="name">
                    <el-input v-model="EquipForm.name"></el-input>
                </el-form-item>
                <el-form-item label="器材数量">
                    <el-input-number size="mini" v-model.number="EquipForm.count" :min="1"
                                     label="人数上限"></el-input-number>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submit" plain>确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {saveEquipAndUploadImage} from '@/utils/apis/equip'
    import upload from "@/utils/methods/upload";

    export default {
        name: "NewEquip",
        data() {
            return {
                //上传图片url
                imgUrl: '',
                HFfileList: [],
                EquipForm: {
                    name: '',
                    count: 5
                },
                dialogVisible: false,
                rules: {
                    name: [
                        {required: true, message: '请输入器材名称', trigger: 'blur'},
                        {min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur'}
                    ]
                },
            }
        },
        props: ['saveNewEquipShow'],
        methods: {
            ...upload,
            handleClose() {
                this.cancel()
            },
            cancel() {
                this.$emit('cancel')
                this.$notify.info('取消添加')
            },
            //提交表单
            submit() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        const equipStr = JSON.stringify(this.EquipForm)
                        const formData = {
                            file: this.file,
                            equipStr: equipStr
                        }
                        saveEquipAndUploadImage(formData).then(res => {
                            if (res.data.code === 200) {
                                this.$notify.success('添加成功')
                                this.$nextTick(() => {
                                    this.EquipForm = {
                                        name: '',
                                        count: 5
                                    }
                                    this.HFhandleRemove()
                                    this.HFfileList = []
                                    this.$emit('cancel')
                                    this.$emit('saveSuccess')
                                })
                            } else {
                                this.$notify.error(res.data.message)
                            }
                        })
                    } else {
                        this.$message.warning('请正确填写表单')
                    }
                })

            },
        },
    }
</script>

<style scoped>

</style>